package com.dst.smart.app;

//import javapns.back.PushNotificationManager;
//import javapns.back.SSLConnectionHelper;
//import javapns.data.Device;
//import javapns.data.PayLoad;


import com.dst.smart.R;
import com.dst.smart.service.SendThread;
import com.dst.smart.tools.AppTools;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

//import com.dst.smart.utils.HanziToPinyin.Token;

public class HomeFragment extends Fragment implements
		RadioGroup.OnCheckedChangeListener, OnClickListener {
	public static int isEdite = 0;
	static CheckBox serialReceived, serialSend, netReceived, netSend,
			remenberName;
	static TextView msgTxt;
	Button cleanTxt;
	Button sendBtn;
	EditText sendTxt;
	private Activity activity;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

	}

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View root = inflater.inflate(R.layout.fragment_home, container, false);
		msgTxt = (TextView) root.findViewById(R.id.msgTxt);
		activity = getActivity();
		serialReceived = (CheckBox) root.findViewById(R.id.serialReceived);
		serialSend = (CheckBox) root.findViewById(R.id.serialSend);
		netReceived = (CheckBox) root.findViewById(R.id.netReceived);
		netSend = (CheckBox) root.findViewById(R.id.netSend);
		remenberName = (CheckBox) root.findViewById(R.id.remenberName);
		cleanTxt = (Button) root.findViewById(R.id.cleanTxt);
		cleanTxt.setOnClickListener(this);
		sendTxt = (EditText) root.findViewById(R.id.sendTxt);
		sendBtn = (Button) root.findViewById(R.id.sendButton);
		sendBtn.setOnClickListener(this);

		return root;
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();

	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		// TODO Auto-generated method stub

	}

	public static void setCode(String result) {
		Message msg = handler.obtainMessage();
		msg.what = 1;
		msg.obj = result;
		handler.sendMessage(msg);
	}

	static String resultStr = "";
	private static Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				int printMsgFlag = 0x0080;
				resultStr = msg.obj.toString();
				if (serialReceived.isChecked() || serialSend.isChecked()
						|| netReceived.isChecked() || netSend.isChecked()
						|| remenberName.isChecked()) {
					printMsgFlag &= 0xff7f;
					if (serialReceived.isChecked()) {
						if (resultStr.startsWith("serialReceived")) {
							printMsgFlag |= 0x0001;
						}
					}
					if (serialSend.isChecked()) {
						if (resultStr.startsWith("serialSend")) {
							printMsgFlag |= 0x0002;
						}
					}
					if (netReceived.isChecked()) {
						if (resultStr.startsWith("netReceived")) {
							printMsgFlag |= 0x0004;
						}
					}
					if (netSend.isChecked()) {
						if (resultStr.startsWith("netSend")) {
							printMsgFlag |= 0x0008;
						}
					}
					if (remenberName.isChecked()) {
						if (resultStr.startsWith("remenberName")) {
							printMsgFlag |= 0x0010;
						}
					}
				}
				if (printMsgFlag > 0) {
					String tempString = msgTxt.getText() + resultStr;
					if (tempString.length() > 800) {
						msgTxt.setText("");
					}
					msgTxt.append(resultStr + "\n");
				}
				break;
			case 3:
				break;
			case 4:
				break;
			}
		}
	};

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.cleanTxt:
			 msgTxt.setText("");
			break;
		case R.id.sendButton:
			String cmd = sendTxt.getText().toString().trim();
			if (!cmd.equals("")) {
				SendThread send = SendThread.getInstance(0,cmd);
				new Thread(send).start();
			}
			break;
		default:
			break;
		}
	}

}
